<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入用户电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡券类型" prop="vipType">
        <el-select v-model="queryParams.vipType" placeholder="卡券类型" clearable>
          <el-option
            v-for="dict in dict.type.vip_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车架号" prop="carVin">
        <el-input
          v-model="queryParams.carVin"
          placeholder="请输入绑定车架号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="vehiclePlate">
        <el-input
          v-model="queryParams.vehiclePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" clearable>
          <el-option
            v-for="dict in dict.type.vip_pay_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付类型" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付类型" clearable>
          <el-option
            v-for="dict in dict.type.vip_pay_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="卡券状态" prop="vipStatus">
        <el-select v-model="queryParams.vipStatus" placeholder="请选择卡券状态" clearable>
          <el-option
            v-for="dict in dict.type.vip_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="卡券来源" prop="sendSource">
        <el-select v-model="queryParams.sendSource" placeholder="请选择卡券来源" clearable>
          <el-option
            v-for="dict in dict.type.send_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderList" border ref="table_show"
              :show-summary="true" :summary-method="getSummaries">
              <el-table-column label="订单号" align="center" prop="orderId" width="220">
        <template slot-scope="scope" class="pre-text">
          <!-- <span style="color:#20B2AA;cursor: pointer;" 
                @click="handleUpdate(scope.row)">{{scope.row.orderId}}</span> -->
                <span style="color:#20B2AA;cursor: pointer;">{{scope.row.orderId}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户手机号" align="center">
      <template slot-scope="scope">  
        <span>{{ scope.row.userPhone.substr(0, 3) + '****' + scope.row.userPhone.substr(-4) }}</span>  
      </template>  
    </el-table-column> 
      <el-table-column label="车架号" align="center" prop="carVin" width="180"/>
      <!-- <el-table-column label="卡券名称" align="center" prop="vipName" width="150" show-overflow-tooltip/> -->
      <el-table-column label="卡券类型" align="center" prop="vipType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vip_type" :value="scope.row.vipType"/>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="orderPrice" />
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vip_pay_status" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="剩余" align="center" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.vipType === 1">{{ '剩余公里：' + (scope.row.usedMiles).toFixed(1) }}</span> 
          <span v-if="scope.row.vipType === 2">{{ '剩余次数：' + (scope.row.usedTime) }}</span> 
          <span v-if="scope.row.vipType === 3">{{ '剩余金额：' + (scope.row.usedValue).toFixed(2) }}</span> 
        </template>
      </el-table-column> -->
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="支付类型" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vip_pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="卡券状态" align="center" prop="vipStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vip_status" :value="scope.row.vipStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="卡券来源" align="center" prop="sendSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.send_source" :value="scope.row.sendSource"/>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <vip-info ref="vipInfo" />
  </div>
</template>

<script>
import { listOrder } from "@/api/zq/vipOrder";
import vipInfo from '@/components/vipInfo/index.vue'
export default {
  name: "Order",
  components: {vipInfo},
  dicts: ['send_source', 'vip_pay_type', 'vip_status', 'vip_type', 'vip_pay_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 卡券订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userPhone: null,
        userName: null,
        vipName: null,
        vipType: null,
        areaId: null,
        vipValue: null,
        usedValue: null,
        miles: null,
        usedMiles: null,
        useTime: null,
        usedTime: null,
        carVin: null,
        vehiclePlate: null,
        payTime: null,
        payStatus: null,
        payType: null,
        vipStatus: null,
        sendSource: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询卡券订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        userPhone: null,
        userName: null,
        vipName: null,
        vipType: null,
        areaId: null,
        vipValue: null,
        usedValue: null,
        miles: null,
        usedMiles: null,
        useTime: null,
        usedTime: null,
        carVin: null,
        vehiclePlate: null,
        createTime: null,
        payTime: null,
        payStatus: null,
        payType: null,
        vipStatus: null,
        sendSource: null
      };
      this.resetForm("form");
    },
      /** 修改按钮操作 */
    handleUpdate(row) {
        this.$refs.vipInfo.getData(row.orderId)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    getSummaries(param) {
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '汇总';
            return;
          }
          // 需要计算汇总的字段
          const includingOptions = [`orderPrice`,`vipValue`]
          const values = data.map(item => Number(item[column.property]));
          if (includingOptions.includes(column.property)) {
            if (!values.every(value => isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return prev + curr;
                } else {
                  return prev;
                }
              }, 0);
              sums[index] = sums[index].toFixed(2);
              sums[index] += '元';
            } else {
              sums[index] = '';
            }
          } else {
            return "";
          }
        });
        return sums;
      },
  }
};
</script>
